package com.dms.modules.user.controller;

import com.dms.common.api.ApiResponse;
import com.dms.common.exception.BusinessException;
import com.dms.common.security.SecurityUtils;
import com.dms.modules.user.dto.AddressDTO;
import com.dms.modules.user.entity.UserAddress;
import com.dms.modules.user.service.UserAddressService;
import com.dms.modules.user.vo.AddressVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/user/address")
@Api(tags = "用户地址接口")
@PreAuthorize("isAuthenticated()")
public class UserAddressController {

    @Autowired
    private UserAddressService userAddressService;

    @GetMapping("/list")
    @ApiOperation("获取地址列表")
    public ApiResponse<List<AddressVO>> getAddressList() {
        Long userId = SecurityUtils.getCurrentUserId();
        return ApiResponse.success(userAddressService.getAddressList(userId));
    }

    @GetMapping("/{addressId}")
    @ApiOperation("获取地址详情")
    public ApiResponse<AddressVO> getAddressDetail(
            @ApiParam("地址ID") @PathVariable Long addressId) {
        Long userId = SecurityUtils.getCurrentUserId();
        return ApiResponse.success(userAddressService.getAddressDetail(userId, addressId));
    }

    @PostMapping
    @ApiOperation("添加地址")
    public ApiResponse<Long> addAddress(
            @Validated @RequestBody AddressDTO addressDTO) {
        Long userId = SecurityUtils.getCurrentUserId();
        return ApiResponse.success(userAddressService.addAddress(userId, addressDTO));
    }

    @PutMapping("/{addressId}")
    @ApiOperation("修改地址")
    public ApiResponse<Void> updateAddress(
            @ApiParam("地址ID") @PathVariable Long addressId,
            @Validated @RequestBody AddressDTO addressDTO) {
        Long userId = SecurityUtils.getCurrentUserId();
        userAddressService.updateAddress(userId, addressId, addressDTO);
        return ApiResponse.success();
    }

    @DeleteMapping("/{addressId}")
    @ApiOperation("删除地址")
    public ApiResponse<Void> deleteAddress(
            @ApiParam("地址ID") @PathVariable Long addressId) {
        Long userId = SecurityUtils.getCurrentUserId();
        userAddressService.deleteAddress(userId, addressId);
        return ApiResponse.success();
    }

    @PutMapping("/{addressId}/default")
    @ApiOperation("设置默认地址")
    public ApiResponse<Void> setDefaultAddress(
            @ApiParam("地址ID") @PathVariable Long addressId) {
        Long userId = SecurityUtils.getCurrentUserId();
        userAddressService.setDefaultAddress(userId, addressId);
        return ApiResponse.success();
    }

    @GetMapping("/default")
    @ApiOperation("获取默认地址")
    public ApiResponse<AddressVO> getDefaultAddress() {
        Long userId = SecurityUtils.getCurrentUserId();
        return ApiResponse.success(userAddressService.getDefaultAddress(userId));
    }
} 